package com.ithinks.flow.common.utils.middleware.rabbitMQ.Topic;

import com.ithinks.flow.common.utils.middleware.rabbitMQ.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @Description
 * @ClassName TopicConsumer01
 * @Author MR.LI
 * @date 2020.11.27 18:41
 */
public class TopicConsumer01 {
    //定义队列名称
    private static final String QUEUE_INFORM_SMS = "queue_topic_sms";
    //指定交换机名称
    private static final String EXCHANGE_TOPIC_INFORM ="exchange_topic_inform";
    //连接配置参数
    private static String IP = "localhost";
    //端口
    private static int PORT = 5672;
    //虚拟机IP，默认/
    private static String VHOST = "/";
    //用户名
    private static String USERNAME = "guest";
    //密码
    private static String PASSWORD = "guest";


    public static void main(String[] args) {

        Connection connection = ConnectionUtils.getConnection(IP, PORT, VHOST, USERNAME, PASSWORD);
        Channel channel = null;

        try {
            channel = connection.createChannel();
            channel.exchangeDeclare(EXCHANGE_TOPIC_INFORM, BuiltinExchangeType.TOPIC);
            channel.queueDeclare(QUEUE_INFORM_SMS,true,false,false,null);
            channel.queueBind(QUEUE_INFORM_SMS,EXCHANGE_TOPIC_INFORM,"inform.sms");

            DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    super.handleDelivery(consumerTag, envelope, properties, body);
                    String message = new String(body,"UTF-8");
                    System.out.println("recived sms message"+message);
                }
            };
            channel.basicConsume(QUEUE_INFORM_SMS,true,defaultConsumer);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
